﻿using MC.Warehouse.ViewModels;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace MC.Warehouse.Core
{
    public class DataGridBehavior
    {
        #region DisplayRowNumber

        public static DependencyProperty DisplayRowNumberProperty =
            DependencyProperty.RegisterAttached("DisplayRowNumber",
                                                typeof (bool),
                                                typeof (DataGridBehavior),
                                                new FrameworkPropertyMetadata(false, 
                                                    OnDisplayRowNumberChanged));
        public static bool GetDisplayRowNumber(DependencyObject target)
        {
            return (bool)target.GetValue(DisplayRowNumberProperty);
        }

        public static void SetDisplayRowNumber(DependencyObject target, bool value)
        {
            target.SetValue(DisplayRowNumberProperty, value);
        }

        private static void OnDisplayRowNumberChanged(DependencyObject target, 
            DependencyPropertyChangedEventArgs e)
        {
            var dataGrid = target as DataGrid;
            if (!((bool) e.NewValue)) return;
            EventHandler<DataGridRowEventArgs> loadedRowHandler = null;
            loadedRowHandler = (sender, ea) =>
                {
                    if (GetDisplayRowNumber(dataGrid) == false)
                    {
                        if (dataGrid != null) dataGrid.LoadingRow -= loadedRowHandler;
                        return;
                    }
                    ea.Row.Header = ea.Row.GetIndex() + 1;
                };
            if (dataGrid != null) dataGrid.LoadingRow += loadedRowHandler;

            ItemsChangedEventHandler itemsChangedHandler = null;
            itemsChangedHandler = (sender, args) =>
                {
                    if (GetDisplayRowNumber(dataGrid) == false)
                    {
                        if (dataGrid != null)
                            dataGrid.ItemContainerGenerator.ItemsChanged -= itemsChangedHandler;
                        return;
                    }
                    GetVisualChildCollection<DataGridRow>(dataGrid).
                        ForEach(d => d.Header = d.GetIndex());
                };
            if (dataGrid != null) dataGrid.ItemContainerGenerator.ItemsChanged += itemsChangedHandler;
        }

        #endregion // DisplayRowNumber

        #region Get Visuals
        private static List<T> GetVisualChildCollection<T>(object parent) where T : Visual
        {
            var visualCollection = new List<T>();
            GetVisualChildCollection(parent as DependencyObject, visualCollection);
            return visualCollection;
        }

        private static void GetVisualChildCollection<T>(DependencyObject parent, ICollection<T> visualCollection)
            where T : Visual
        {
            var count = VisualTreeHelper.GetChildrenCount(parent);
            for (var i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                if (child is T)
                {
                    visualCollection.Add(child as T);
                }
                if (child != null)
                {
                    GetVisualChildCollection(child, visualCollection);
                }
            }
        }
        #endregion // Get Visuals

        #region DataGridRow double click
        private static RelayCommand _rowDoubleClick;
        public static RelayCommand RowDoubleClick
        {
            get
            {
                return _rowDoubleClick ?? (_rowDoubleClick = new RelayCommand(param =>
                    {
                        var userControlViewModel = TabbarManager.UcSelected.DataContext as UserControlViewModel;
                        if (userControlViewModel != null)
                            userControlViewModel.Edit();
                    }));
            }
        }

        internal static void Clean()
        {
            _rowDoubleClick = null;
        }

        #endregion //DataGridRow double click
    }
}
